/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.core.hotkey.win32;

import java.util.ArrayList;
import java.util.List;

import org.kommando.core.hotkey.AbstractHotkeyService;
import org.kommando.core.hotkey.Hotkey;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

public class JIntellitypeHotkeyService extends AbstractHotkeyService {

	private JIntellitype intellitype;
	private List<Hotkey> keys = new ArrayList<Hotkey>();

	public JIntellitypeHotkeyService() {
		intellitype = JIntellitype.getInstance();
		intellitype.addHotKeyListener(new HotkeyListener() {
			@Override
			public void onHotKey(int id) {
				hotkeyPressed(keys.get(id));
			}
		});
	}

	@Override
	public void registerHotkey(Hotkey key) {
		int id = keys.size();
		keys.add(key);
		intellitype.registerSwingHotKey(id, key.getModifier(), key.getKey());
	}

	@Override
	public void unregisterHotkey(Hotkey key) {
		int id = keys.indexOf(key);
		keys.set(id, null);
		intellitype.unregisterHotKey(id);
	}
}
